package com.ruoyi.safe.controller.web;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizDutyRegion;
import com.ruoyi.safe.domain.vo.BizDutyRegionVO;
import com.ruoyi.safe.service.IBizDutyRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 责任区域Controller
 *
 * @author buxi
 * @date 2023-11-11
 */
@RestController
@RequestMapping("/duty/region")
public class BizDutyRegionController extends BaseController {
    @Autowired
    private IBizDutyRegionService bizDutyRegionService;

    /**
     * 查询责任区域列表
     */
    @PreAuthorize("@ss.hasPermi('duty:region:list')")
    @GetMapping("/list")
    public AjaxResult list(BizDutyRegion bizDutyRegion) {
        List<BizDutyRegionVO> list = bizDutyRegionService.selectBizDutyRegionListVO(bizDutyRegion);
        return success(list);
    }

    /**
     * 导出责任区域列表
     */
    @PreAuthorize("@ss.hasPermi('duty:region:export')")
    @Log(title = "责任区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizDutyRegion bizDutyRegion) {
        List<BizDutyRegion> list = bizDutyRegionService.selectBizDutyRegionList(bizDutyRegion);
        ExcelUtil<BizDutyRegion> util = new ExcelUtil<>(BizDutyRegion.class);
        util.exportExcel(response, list, "责任区域数据");
    }

    /**
     * 获取责任区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('duty:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizDutyRegionService.selectBizDutyRegionById(id));
    }

    /**
     * 新增责任区域
     */
    @PreAuthorize("@ss.hasPermi('duty:region:add')")
    @Log(title = "责任区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDutyRegion bizDutyRegion) {
        return toAjax(bizDutyRegionService.insertBizDutyRegion(bizDutyRegion));
    }

    /**
     * 修改责任区域
     */
    @PreAuthorize("@ss.hasPermi('duty:region:edit')")
    @Log(title = "责任区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDutyRegion bizDutyRegion) {
        return toAjax(bizDutyRegionService.updateBizDutyRegion(bizDutyRegion));
    }

    /**
     * 删除责任区域
     */
    @PreAuthorize("@ss.hasPermi('duty:region:remove')")
    @Log(title = "责任区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizDutyRegionService.deleteBizDutyRegionByIds(ids));
    }

    /**
     * 上移下移
     */
    @GetMapping("/move")
    public AjaxResult move(int moveType, Long id) {
        return toAjax(bizDutyRegionService.move(moveType, id));
    }

    /**
     * 获取责任区域树
     * @param bizDutyRegion
     * @return
     */
    @GetMapping("/region/tree")
    public AjaxResult getRegionList(BizDutyRegion bizDutyRegion) {
        List<BizDutyRegion> bizDutyRegions = bizDutyRegionService.selectBizDutyRegionList(bizDutyRegion);
        List<TreeNode<String>> nodeList = bizDutyRegions.stream()
                .map(bizDutyRegion1 -> {
                    TreeNode<String> treeNode = new TreeNode<>();
                    treeNode.setId(String.valueOf(bizDutyRegion1.getId()));
                    treeNode.setParentId(String.valueOf(bizDutyRegion1.getParentId()));
                    treeNode.setName(bizDutyRegion1.getRegionName());
                    treeNode.setWeight(bizDutyRegion1.getSort());
                    return treeNode;
                })
                .collect(Collectors.toList());
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0");

        return success(treeNodes);
    }
}
